//
//  loginView.m
//  zhangGuangTianXia
//
//  Created by tangtangchitangtang on 2017/11/7.
//  Copyright © 2017年 tangtangchitangtang. All rights reserved.
//

#import "loginView.h"
#import "forgetViewController.h"

@implementation loginView

-(void)creatView{
    UIImageView *imageView=[UIImageView new];
    [self addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    

    
    UIView *textView=[UIView new];
    [self addSubview:textView];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(130);
    }];
    textView.backgroundColor=ColorWithRGB(240, 240, 240);
    textView.layer.cornerRadius=25;
   
    
    _phoneText=[UITextField new];
    [textView addSubview:_phoneText];
    [_phoneText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.height.mas_equalTo(50);
        make.centerY.mas_equalTo(0);
    }];
    _phoneText.placeholder=@"请输入手机号码";
    _phoneText.textColor=ColorWithRGB(49, 49, 49);
    _phoneText.font=[UIFont systemFontOfSize:17];
    _phoneText.returnKeyType=UIReturnKeyDone;
    _phoneText.keyboardType=UIKeyboardTypePhonePad;
    
    UIView *textView2=[UIView new];
    [self addSubview:textView2];
    [textView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(50);
        make.top.mas_equalTo(textView.mas_bottom).mas_equalTo(20);
    }];
    textView2.backgroundColor=ColorWithRGB(240, 240, 240);
    textView2.layer.cornerRadius=25;
    
    
    UILabel *label=[UILabel new];
    [textView2 addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-25);
        make.centerY.mas_equalTo(0);
    }];
    label.textColor=_tianLanColor;
    label.font=[UIFont systemFontOfSize:14];
    label.text=@"忘记密码";
    [label layoutIfNeeded];
    label.userInteractionEnabled=YES;
    UITapGestureRecognizer *wangJi = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushToWangJi:)];
    [label addGestureRecognizer:wangJi];
    
    _passwordText=[UITextField new];
    [textView2 addSubview:_passwordText];
    [_passwordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(label.mas_left).mas_equalTo(-10);
        make.height.mas_equalTo(50);
        make.centerY.mas_equalTo(0);
    }];
    _passwordText.placeholder=@"请输入密码";
    _passwordText.textColor=ColorWithRGB(49, 49, 49);
    _passwordText.font=[UIFont systemFontOfSize:17];
    _passwordText.keyboardType=UIKeyboardTypeNamePhonePad;
    _passwordText.delegate=self;
    _passwordText.secureTextEntry=YES;//密文
    _passwordText.returnKeyType=UIReturnKeyDone;
    
    
    UIButton *btn=[UIButton new];
    [self addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(textView2);
        make.top.mas_equalTo(textView2.mas_bottom).mas_equalTo(40);
        make.height.mas_equalTo(50);
    }];
    btn.backgroundColor=_tianLanColor;
    [btn setTitleColor:[UIColor whiteColor] forState:0];
    [btn setTitle:@"登录" forState:0];
    btn.titleLabel.font=[UIFont systemFontOfSize:17];
    btn.layer.cornerRadius=25;
    [btn addTarget:self action:@selector(loginBtn:) forControlEvents:UIControlEventTouchUpInside];
    
}

-(IBAction)loginBtn:(id)sender{
    
    NSString *phoneStr=_phoneText.text;
    if (![ZQTools PhoneModel:phoneStr]) {
        [ZQTools svpInfo:@"请输入正确的手机号"];
        return;
    }
    NSString *passwordStr=_passwordText.text;
    if (passwordStr.length<6||passwordStr.length>17) {
        [ZQTools svpInfo:@"请输入正确的密码"];
        return;
    }

    
    NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:phoneStr,@"phone",passwordStr,@"password",@"1",@"operatesys", nil];
    
    [ZQTools AFNPostDataUrl:@"suser/userLogin" Dict:dic andTableView:nil andView:self andSuccessBlock:^(id responseObject) {
        BaseModel *dataModel = [BaseModel mj_objectWithKeyValues:responseObject];
        if ([dataModel.status isEqualToString:@"200"]) {
            [NSKeyedArchiver archiveRootObject:dataModel.data toFile:_userModelFile];
            
            if (_fanhui==2) {
                int index = (int)[[self.viewController.navigationController viewControllers]indexOfObject:self.viewController];
                [self.viewController.navigationController popToViewController:[self.viewController.navigationController.viewControllers objectAtIndex:(index-2)] animated:YES];
            }
            else{
                [self.viewController.navigationController popViewControllerAnimated:YES];
            }
          
            
        }
        else{
            [ZQTools svpInfo:dataModel.msg];
        }
        
    } anderrorBlock:nil];




}


-(void)pushToWangJi:(UITapGestureRecognizer *)tap{
    forgetViewController *forget=[[forgetViewController alloc] init];
    [ZQTools pushNextViewController:self.viewController andRootController:forget];
}




- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string{
    if ([[[UITextInputMode currentInputMode]primaryLanguage] isEqualToString:@"emoji"]) {
        return NO;
    }
    return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

@end
